<?php
/**
 * @file album.php
 * @author LJ (liangjian@haibao.com)
 * @date 2015年11月24日 下午7:20:24
 * @brief 相册业务层
 */
namespace haibao\app\business;

class Album extends \haibao\app\business\Base {

    /**
     * @var $_instance
     */
    private static $_instance;
    
    /**
     * instance()
     */
    private static function instance(){
        if(!self::$_instance){
            self::$_instance = new \haibao\app\data\Album();
        }
        return self::$_instance;
    }
    
    /**
     * 查询一个相册
     */
    public static function getAlbumById($albumId){
        return self::instance()->getOneById($albumId);
    }
    
    /**
     * 用户Id和相册是否匹配
     */
    public static function pregAlbum($userId, $albumId){
        return self::instance()->pregAlbum($userId, $albumId);
    }
    
    /**
     * 修改相册名称
     */
    public static function updateAlbumName($userId, $albumId, $name){
        if(!$userId){
            throw new BusinessException('用户Id不能为空', self::DATA_ERROR_CODE_ALBUM_USER_ID);
        }
        if(!$albumId){
            throw new BusinessException('相册Id不能为空', self::DATA_ERROR_CODE_ALBUM_ID);
        }
        if(!$name){
            throw new BusinessException('相册名不能为空', self::DATA_ERROR_CODE_ALBUM_NAME);
        }
        if(!self::pregAlbum($userId, $albumId)){
            throw new BusinessException('用户与相册不匹配', self::DATA_ERROR_CODE_ALBUM_MISMATCH);
        }
        $album = self::getAlbumByName($userId, $name);
        if($album && $album['Id'] != $albumId && $album['Name'] == $name){
            throw new BusinessException('相册名不能重复', self::DATA_ERROR_CODE_ALBUM_NAME_EXIST);
        }
        
        $album = self::getAlbumById($albumId);
        if($album && $album->UserId != $userId){
            throw new BusinessException('非法操作', self::DATA_ERROR_CODE_ALBUM_ILLEGAL_USER);
        }
        if(empty($album)){
            throw new BusinessException('相册不存在', self::DATA_ERROR_CODE_ALBUM_NOT_EXIST);
        }
        self::instance()->updateAlbumName($userId,$albumId, $name);
    }
    
    /**
     * 删除相册
     */
    public static function deleteAlbum($userId, $albumId){
        if(!$userId){
            throw new BusinessException('用户Id不能为空', self::DATA_ERROR_CODE_ALBUM_USER_ID);
        }
        if(!$albumId){
            throw new BusinessException('相册Id不能为空', self::DATA_ERROR_CODE_ALBUM_ID);
        }
        if(!self::pregAlbum($userId, $albumId)){
            throw new BusinessException('用户与相册不匹配', self::DATA_ERROR_CODE_ALBUM_MISMATCH);
        }
        self::instance()->deleteAlbum($userId, $albumId);
    }
    
    public static function getAlbumByName($userId, $name){
        if(!$userId){
            throw new BusinessException('用户Id不能为空', self::DATA_ERROR_CODE_ALBUM_USER_ID);
        }
        if(!$name){
            throw new BusinessException('相册名称不能为空', self::DATA_ERROR_CODE_ALBUM_NAME);
        }
        return self::instance()->getAlbumByName($userId, $name);
    }
    
    /**
     * 根据用户Id查询相册列表
     * @param int $userId 用户Id
     * @param boolean $isFromArticle 是否来自文章大图预览，如果来自文章则过滤曾经收藏相册
     * @throws BusinessException
     */
    public static function getAlbumByUserId($userId, $isFromArticle = false){
        if(!$userId){
            throw new BusinessException('用户Id不能为空', self::DATA_ERROR_CODE_ALBUM_USER_ID);
        }
        $albumList = self::instance()->getAlbumList($userId);
        if($albumList){
            $albumIdArr = array();
            $albumListTmp = array();
            foreach ($albumList as $key=>$album){
                array_push($albumListTmp, $album);
                array_push($albumIdArr, $album['albumId']);
            }
            $albumList = $albumListTmp;
            $imageList = self::getAlbumRelImage($userId, $albumIdArr);
            
            $tmpList = array();
            foreach($albumList as $key=>$value){
                if($isFromArticle && $value['albumType'] == \haibao\app\model\data\Album::ALBUM_TYPE_COLLECT){
                    continue;
                }
                array_push($tmpList, $value);
            }
            
            $albumList = $tmpList;
            foreach ($albumList as $key=>$album){
                $albumList[$key]['imageList'] = array_key_exists($album['albumId'], $imageList) ? $imageList[$album['albumId']] : array();
                unset($albumList[$key]['albumType']);
            }
        }
        return $albumList;
    }
    
    /**
     * 筛选相册列表
     */
    public static function getAlbumByQuery($query){
        return self::instance()->getAll($query);
    }
    
    /**
     * 创建默认相册
     */
    public static function createDefaultAlbum($userId){
        $model = new \haibao\app\model\data\Album();
        $model->Name = '默认相册';
        $model->UserId = $userId;
        self::createAlbum($model, \haibao\app\model\data\Album::ALBUM_TYPE_DEFAULT);
    }
    
    /**
     * 创建相册
     * @param \haibao\app\model\data\Album $model
     */
    public static function createAlbum($model, $type = null){
        if(!$model->Name){
            throw new BusinessException('相册名称不能为空', self::DATA_ERROR_CODE_ALBUM_NAME);
        }
        if(!$model->UserId){
            throw new BusinessException('非法的所属用户', self::DATA_ERROR_CODE_ALBUM_ILLEGAL_USER);
        }
        if(self::getAlbumByName($model->UserId, $model->Name)){
            throw new BusinessException('相册名不能重复', self::DATA_ERROR_CODE_ALBUM_NAME_EXIST);
        }
        $albumCount = self::instance()->getAlbumCount($model->UserId);
        if($albumCount >= \haibao\classlibrary\app\Config::getConfig(\haibao\classlibrary\app\Config::ABLUM_COUNT_MAX)){
            throw new BusinessException('最多可创建'.\haibao\classlibrary\app\Config::getConfig(\haibao\classlibrary\app\Config::ABLUM_COUNT_MAX).'个相册', self::DATA_ERROR_CODE_ALBUM_OVER_LIMIT);
        }
        $model->ImageCount = 0;
        $model->Status = \haibao\app\model\data\Album::STATUS_ENABLE;
        $model->CreateTime = new \DateTime();
        self::instance()->createAlbum($model, $type);
        return $model->Id;
    }
    
    /**
     * 相册关联的最新三张图片
     */
    public static function getAlbumRelImage($userId, array $idArr){
        if(!$idArr){
            return false;
        }
        $albumImage = self::instance()->getAlbumTop3($userId);
        if(!$albumImage){
            foreach ($idArr as $albumId){
                $albumImage[$albumId] = \haibao\app\business\Collection::getCollection($albumId, null,  3);
            }
            self::instance()->setAlbumTop3($userId, $albumImage);
        }
        return $albumImage;
    }
    
    /**
     * 是否创建过默认相册或收藏相册
     */
    public static function getCreateAlbumLog($userId, $type = \haibao\app\model\data\Album::ALBUM_TYPE_DEFAULT){
        $defaultAlbum = self::instance()->getCreateAlbumLog($userId, $type);
        return !empty($defaultAlbum) ? true : false;
    }
    
} 